﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;
using System.Data.SqlClient;
using AspNetCs.Config;
using System.Text;
public partial class HeadControl : System.Web.UI.UserControl
{
    //上一页
    protected string prePage = "";
    //下一页
    protected string nextPage = "";
    protected string inputPageIndex = "";

    //页码

    protected string pageNum = "";

    private int pageSize = 4;
    private LoveWallBLL loveWallBLL = new LoveWallBLL();

    public int pageIndex = 1;

    //查询记录的条数
    public int totalCount = 0;

    //总页数
    protected int totalPage = 0;
    
    public string keyWord = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        pageInit();
    }

    //页面初始
    protected void pageInit()
    {

        if (Request["keyWord"] != null)
        {
            keyWord = Request["keyWord"].ToString();
        }

        if (Request["pageIndex"] != null)
        {
            try
            {
                pageIndex = Convert.ToInt32(Request["pageIndex"]);
            }
            catch (Exception ex)
            {
                pageIndex = 0;
                new CustomException(ex);
            }
        }


        SqlDataReader reader = loveWallBLL.getGetLoveWallMsg(keyWord, pageIndex, pageSize, 1);
        if (reader.Read())
        {
            totalCount = reader.GetInt32(0);
        }
        reader.Close();


        if(totalCount<=0)
        {
            return;
        }

        totalPage=getTotalPage();
        int showCount = 10;

        
        if (pageIndex > 1 && totalCount != 0 && pageIndex <=totalPage)
        {
            prePage = "<a href=\"javascript:drePageIndex(1);\">上一页</a>";
        }
        if (totalPage != pageIndex && totalCount != 0 && pageIndex <= totalPage)
        {
            nextPage = "<a href=\"javascript:addPageIndex(1);\">下一页</a>";       
        }
        if (totalPage < 10)
        {
            showCount = totalPage;
        }

        for (int i = 1; i <=showCount;i++ )
        {
            pageNum += "&nbsp;&nbsp;<a href=\"javascript:getPage(" + i + ");\">" + i + "</a>";
        }
        
        if(totalCount!=0)
        {
            inputPageIndex = "&nbsp;页码&nbsp;<input size=\"5\" type=\"text\" onkeyup=\"inputPageIndex();\" value=\"" + pageIndex + "\" id=\"txtPageNum\" />";
        }
    }
    /// <summary>
    /// 获得总页数
    /// </summary>
    /// <returns></returns>
    private int getTotalPage()
    {
        int totalPageCount = 0;
        if (pageSize!=0)
        {
            if (totalCount % pageSize == 0)
            {
                totalPageCount = totalCount / pageSize;
            }
            else
            {
                totalPageCount = (totalCount / pageSize) + 1;
            }
        }
        return totalPageCount;
    }
}
